home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / daemons / nfs / nfs-serv.2be / nfs-serv / nfs-server-2.2beta16 / showmount.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-12  |  6.2 KB  |  299 lines

  1. /*
  2.  * showmount.c -- show mount information for an NFS server
  3.  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  */
  15.  
  16. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19.  
  20. #include <stdio.h>
  21. #include <rpc/rpc.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <sys/time.h>
  25. #ifdef HAVE_STRING_H
  26. #include <string.h>
  27. #else
  28. #include <strings.h>
  29. #endif
  30. #include <sys/types.h>
  31. #ifdef STDC_HEADERS
  32. #include <unistd.h>
  33. #include <memory.h>
  34. #include <stdlib.h>
  35. #endif
  36.  
  37. #include <netdb.h>
  38. #include <arpa/inet.h>
  39. #include <errno.h>
  40. #include "getopt.h"
  41.  
  42. #include "mount.h"
  43.  
  44. int headers = 1;
  45. int hflag = 0;
  46. int aflag = 0;
  47. int dflag = 0;
  48. int eflag = 0;
  49.  
  50. extern char version[];
  51. char *program_name;
  52. static struct option longopts[] =
  53. {
  54.     { "all", 0, 0, 'a' },
  55.     { "directories", 0, 0, 'd' },
  56.     { "exports", 0, 0, 'e' },
  57.     { "no-headers", 0, &headers, 'n'},
  58.     { "version", 0, 0, 'v' },
  59.     { "help", 0, 0, 'h' },
  60.     { NULL, 0, 0, 0 }
  61. };
  62.  
  63. #define MAXHOSTLEN 256
  64.  
  65. int dump_cmp(p, q)
  66. char **p;
  67. char **q;
  68. {
  69.     return strcmp(*p, *q);
  70. }
  71.  
  72. static void usage(fp, n)
  73. FILE *fp;
  74. int n;
  75. {
  76.     fprintf(fp, "Usage: %s [-adehv]\n", program_name);
  77.     fprintf(fp, "       [--all] [--directories] [--exports]\n");
  78.     fprintf(fp, "       [--no-headers] [--help] [--version] [host]\n");
  79.     exit(n);
  80. }
  81.  
  82. int main(argc, argv)
  83. int argc;
  84. char **argv;
  85. {
  86.     char hostname_buf[MAXHOSTLEN];
  87.     char *hostname;
  88.     enum clnt_stat clnt_stat;
  89.     struct hostent *hp;
  90.     struct sockaddr_in server_addr;
  91.     int msock;
  92.     struct timeval total_timeout;
  93.     struct timeval pertry_timeout;
  94.     int c;
  95.     CLIENT *mclient;
  96.     groups grouplist;
  97.     exports exportlist, exl;
  98.     mountlist dumplist;
  99.     mountlist list;
  100.     int i;
  101.     int n;
  102.     int maxlen;
  103.     char **dumpv;
  104.  
  105.     program_name = argv[0];
  106.     while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
  107.         switch (c) {
  108.         case 'a':
  109.             aflag = 1;
  110.             break;
  111.         case 'd':
  112.             dflag = 1;
  113.             break;
  114.         case 'e':
  115.             eflag = 1;
  116.             break;
  117.         case 'h':
  118.             usage(stdout, 0);
  119.             break;
  120.         case 'v':
  121.             printf("%s\n", version);
  122.             exit(0);
  123.         case 0:
  124.             break;
  125.         case '?':
  126.         default:
  127.             usage(stderr, 1);
  128.             break;
  129.         }
  130.     }
  131.     argc -= optind;
  132.     argv += optind;
  133.  
  134.     switch (aflag + dflag + eflag) {
  135.     case 0:
  136.         hflag = 1;
  137.         break;
  138.     case 1:
  139.         break;
  140.     default:
  141.         fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
  142.             program_name);
  143.         exit(1);
  144.         break;
  145.     }
  146.  
  147.     switch (argc) {
  148.     case 0:
  149.         if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
  150.             perror("getting hostname");
  151.             exit(1);
  152.         }
  153.         hostname = hostname_buf;
  154.         break;
  155.     case 1:
  156.         hostname = argv[0];
  157.         break;
  158.     default:
  159.         fprintf(stderr, "%s: only one hostname is allowed\n",
  160.             program_name);
  161.         exit(1);
  162.         break;
  163.     }
  164.  
  165.     if (hostname[0] >= '0' && hostname[0] <= '9') {
  166.         server_addr.sin_family = AF_INET;
  167.         server_addr.sin_addr.s_addr = inet_addr(hostname);
  168.     }
  169.     else {
  170.         if ((hp = gethostbyname(hostname)) == NULL) {
  171.             fprintf(stderr, "%s: can't get address for %s\n",
  172.                 program_name, hostname);
  173.             exit(1);
  174.         }
  175.         server_addr.sin_family = AF_INET;
  176.         memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
  177.     }
  178.  
  179.     /* create mount deamon client */
  180.  
  181.     server_addr.sin_port = 0;
  182.     msock = RPC_ANYSOCK;
  183.     if ((mclient = clnttcp_create(&server_addr,
  184.         MOUNTPROG, MOUNTVERS, &msock, 0, 0)) == NULL) {
  185.         server_addr.sin_port = 0;
  186.         msock = RPC_ANYSOCK;
  187.         pertry_timeout.tv_sec = 3;
  188.         pertry_timeout.tv_usec = 0;
  189.         if ((mclient = clntudp_create(&server_addr,
  190.             MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
  191.             clnt_pcreateerror("mount clntudp_create");
  192.             exit(1);
  193.         }
  194.     }
  195.     mclient->cl_auth = authunix_create_default();
  196.     total_timeout.tv_sec = 20;
  197.     total_timeout.tv_usec = 0;
  198.  
  199.     if (eflag) {
  200.         memset(&exportlist, '\0', sizeof(exportlist));
  201.         clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
  202.             (xdrproc_t) xdr_void, NULL,
  203.             (xdrproc_t) xdr_exports, &exportlist,
  204.             total_timeout);
  205.         if (clnt_stat != RPC_SUCCESS) {
  206.             clnt_perror(mclient, "rpc mount export");
  207.             exit(1);
  208.         }
  209.         if (headers)
  210.             printf("Export list for %s:\n", hostname);
  211.         maxlen = 0;
  212.         for (exl = exportlist; exl; exl = exl->ex_next) {
  213.             if ((n = strlen(exl->ex_dir)) > maxlen)
  214.                 maxlen = n;
  215.         }
  216.         while (exportlist) {
  217.             printf("%-*s ", maxlen, exportlist->ex_dir);
  218.             grouplist = exportlist->ex_groups;
  219.             if (grouplist)
  220.                 while (grouplist) {
  221.                     printf("%s%s", grouplist->gr_name,
  222.                         grouplist->gr_next ? "," : "");
  223.                     grouplist = grouplist->gr_next;
  224.                 }
  225.             else
  226.                 printf("(everyone)");
  227.             printf("\n");
  228.             exportlist = exportlist->ex_next;
  229.         }
  230.         exit(0);
  231.     }
  232.  
  233.     memset(&dumplist, '\0', sizeof(dumplist));
  234.     clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
  235.         (xdrproc_t) xdr_void, NULL,
  236.         (xdrproc_t) xdr_mountlist, &dumplist,
  237.         total_timeout);
  238.     if (clnt_stat != RPC_SUCCESS) {
  239.         clnt_perror(mclient, "rpc mount dump");
  240.         exit(1);
  241.     }
  242.  
  243.     n = 0;
  244.     for (list = dumplist; list; list = list->ml_next)
  245.         n++;
  246.     dumpv = (char **) calloc(n, sizeof (char *));
  247.     if (n && !dumpv) {
  248.         fprintf(stderr, "%s: out of memory\n", program_name);
  249.         exit(1);
  250.     }
  251.     i = 0;
  252.  
  253.     if (hflag) {
  254.         if (headers)
  255.             printf("Hosts on %s:\n", hostname);
  256.         while (dumplist) {
  257.             dumpv[i++] = dumplist->ml_hostname;
  258.             dumplist = dumplist->ml_next;
  259.         }
  260.     }
  261.     else if (aflag) {
  262.         if (headers)
  263.             printf("All mount points on %s:\n", hostname);
  264.         while (dumplist) {
  265.             char s[1024];
  266.             char *t;
  267.  
  268.             sprintf(s, "%s:%s", dumplist->ml_hostname,
  269.                 dumplist->ml_directory);
  270.             t = malloc(strlen(s) + 1);
  271.             if (t)
  272.                 strcpy(t, s);
  273.             else {
  274.                 printf("%s: out of memory\n", program_name);
  275.                 exit(1);
  276.             }
  277.             dumpv[i++] = t;
  278.             dumplist = dumplist->ml_next;
  279.         }
  280.     }
  281.     else if (dflag) {
  282.         if (headers)
  283.             printf("Directories on %s:\n", hostname);
  284.         while (dumplist) {
  285.             dumpv[i++] = dumplist->ml_directory;
  286.             dumplist = dumplist->ml_next;
  287.         }
  288.     }
  289.  
  290.     qsort(dumpv, n, sizeof (char *), dump_cmp);
  291.     
  292.     for (i = 0; i < n; i++) {
  293.         if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
  294.             printf("%s\n", dumpv[i]);
  295.     }
  296.     exit(0);
  297. }
  298.  
  299.